package com.tang.calculator.symbols;

import com.tang.calculator.context.CalculateContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Stack;

/**
 * @ClassName: AndSymbol
 * @Author: zhiy
 * @Date: 2021/3/17 14:38
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class AndSymbol implements OperatorSymbol{
    @Autowired
    ValueSymbolFactory valueSymbolFactory;

    @Override
    public int getPriority() {
        return 15;
    }

    @Override
    public void simpleOperate(CalculateContext context) {
        Stack<ValueSymbol> valueStack = context.getValueStack();
        ValueSymbol value1 = valueStack.pop();
        ValueSymbol value2 = valueStack.pop();
        BigDecimal res;
        if (value1.getValue().compareTo(BigDecimal.ZERO) == 0 || value2.getValue().compareTo(BigDecimal.ZERO) == 0) {
            res = new BigDecimal("0");
        } else {
            res = new BigDecimal("1");
        }
        ValueSymbol valueSymbol = valueSymbolFactory.create(res.toString());

        valueStack.push(valueSymbol);
    }

    @Override
    public String getName() {
        return "&&";
    }
}
